package com.hospital.pet.love.lovepethospital.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.pet.love.lovepethospital.domain.Drug;
import com.hospital.pet.love.lovepethospital.domain.Employee;
import com.hospital.pet.love.lovepethospital.dto.drug.AddDrugDto;
import com.hospital.pet.love.lovepethospital.dto.drug.RefillSuppliesDto;
import com.hospital.pet.love.lovepethospital.exp.ShawCustomException;
import com.hospital.pet.love.lovepethospital.mapper.EmployeeMapper;
import com.hospital.pet.love.lovepethospital.resp.ResultCodeEnum;
import com.hospital.pet.love.lovepethospital.service.DrugService;
import com.hospital.pet.love.lovepethospital.mapper.DrugMapper;
import com.hospital.pet.love.lovepethospital.util.AuthenticationContextUtil;
import com.hospital.pet.love.lovepethospital.util.RoleJudgeUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @author RoselleShaw
 * @description 针对表【drug】的数据库操作Service实现
 * @createDate 2024-04-26 20:05:41
 */
@Service
public class DrugServiceImpl extends ServiceImpl<DrugMapper, Drug>
        implements DrugService {

    @Resource
    private DrugMapper drugMapper;
    @Resource
    private EmployeeMapper employeeMapper;

    private void druggistJudge() {
        RoleJudgeUtil.employeeJudge();
        if (!"药剂师".equals(employeeMapper.selectOne(
                new LambdaQueryWrapper<Employee>()
                        .eq(Employee::getPhone, AuthenticationContextUtil.get().getPhone())
        ).getJob())) {
            throw new ShawCustomException(ResultCodeEnum.NOT_EMPLOYEE);
        }
    }

    @Override
    public void save(AddDrugDto addDrugDto) {
        druggistJudge();
        Drug drug = new Drug();
        BeanUtil.copyProperties(addDrugDto, drug);
        drugMapper.insert(drug);
    }

    @Override
    public void refillSupplies(RefillSuppliesDto refillSuppliesDto) {
        druggistJudge();
        Drug drug = drugMapper.selectById(refillSuppliesDto.getDrugId());
        if (drug == null) {
            throw new ShawCustomException(ResultCodeEnum.DRUG_NOT_EXIST);
        }
        drug.setDrugQuantity(drug.getDrugQuantity() + refillSuppliesDto.getDrugQuantity());
        drugMapper.updateById(drug);
    }
}




